package io.renren.util;




import cn.hutool.crypto.SecureUtil;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;


/**
 * MD5加密
 * 
 * @version 1.0
 * @author
 * 
 */
public class MD5 {

    public static String byteToHEX(byte ib) {


        char[] DigitNormal = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

                'a', 'b', 'c', 'd', 'e', 'f' };



        /*
         * char[] DigitCap = { '0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
         */

        char[] ob = new char[2];

        ob[0] = DigitNormal[(ib >>> 4) & 0X0F];

        ob[1] = DigitNormal[ib & 0X0F];


        return new String(ob);

    }


    /**
     * MD5加密
     * 
     * @param plainText
     * @return
     */
    public static String generateMD5String(String plainText) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();

            int i;
            StringBuffer sb = new StringBuffer();
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    sb.append("0");
                }
                sb.append(Integer.toHexString(i));
            }
            return sb.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            // log.error...
            System.out.println("generate MD5 String error! " + e.getMessage());
            // throw e;
        }
        return null;
    }
    public static String md5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] byteDigest = md.digest();
            int i;
            //字符数组转换成字符串
            StringBuffer buf = new StringBuffer();
            for (int offset = 0; offset < byteDigest.length; offset++) {
                i = byteDigest[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            // 32位加密
            return buf.toString().toUpperCase();
            // 16位的加密
            //return buf.toString().substring(8, 24).toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String threeMd5(String src) {
        String s1 = SecureUtil.md5(src).toUpperCase();//计算MD5并转大写

         String s2 = SecureUtil.md5(s1).toUpperCase();//计算MD5并转大写

         return SecureUtil.md5(s2).toUpperCase();//计算MD5并转大写

      }


        public static String getMD5(String source) {
        System.out.println(source);
        String s = null;
        char hexDigits[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            md.update(source.getBytes());
            byte tmp[] = md.digest();

            char str[] = new char[16 * 2];

            int k = 0; // 表示转换结果中对应的字符位置
            for (int i = 0; i < 16; i++) {

                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];

                str[k++] = hexDigits[byte0 & 0xf];
            }
            s = new String(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }


    // 19pay提供的MD5加密方式-仅限手机充值接口使用--begin

    public static String getKeyedDigest(String strSrc, String key) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(strSrc.getBytes(StandardCharsets.UTF_8));

            String result = "";
            byte[] temp;
            temp = md5.digest(key.getBytes(StandardCharsets.UTF_8));
            for (int i = 0; i < temp.length; i++) {
                result += Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
            }

            return result;

        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String Sign(Map<String, Object> params ) {


        Object[] keys = params.keySet().toArray();
        Arrays.sort(keys);
        // 计算并添加签名
        StringBuilder sb = new StringBuilder();

        for (Object key : keys) {
            sb.append(params.get((String) key));
        }


        return sb.toString();
    }




    /*public static void main(String[] args) {
        // TODO Auto-generated method stub
        String mi;
        String s = "123456";
        // 第二个参数请填空字符串
        mi = MD5.getMD5(s);
        System.out.println("mi:" + mi);

    }*/

}
